package LeetCode.leetcode17;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public static final List<String> NUM = Arrays.asList(new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"});

    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        if (digits.length() == 0)
            return res;
        backTracking(digits, 0, sb, res);
        return res;
    }

    public static void backTracking(String digits, int startIndex, StringBuilder sb, List<String> res) {

        if (sb.length() == digits.length()) {
            res.add(new String(sb));
            return;
        }

        if (startIndex >= digits.length()) {
            return;
        }
        int k = Integer.parseInt(String.valueOf(digits.charAt(startIndex)));
        String tmp = NUM.get(k);
        for (int j = 0; j < tmp.length(); j++) {
            sb.append(tmp.charAt(j));
            backTracking(digits, startIndex + 1, sb, res);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}

